// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (The system cannot find the path specified))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.ba360.portal.common.ErrorMessages;
import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.struts.form.YPListingsForm;

/**
 * ViewDetails.java created by EasyStruts - XsltGen. http://easystruts.sf.net
 * created on 06-28-2004
 * 
 * XDoclet definition:
 * 
 * @struts:action validate="true"
 * @struts:action-forward name="/jsp/ViewDetails.jsp"
 *                        path="/jsp/ViewDetails.jsp"
 */
public class ViewDetails extends Action {

	// --------------------------------------------------------- Instance
	// Variables

	// --------------------------------------------------------- Methods

	/**
	 * Method execute
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 * @throws Exception
	 */


	private MessageResources errMsg = null;

	private String ResourceKey = "ErrorMessages";

	public ViewDetails() {
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		errMsg = getResources(request, ResourceKey);
		HttpSession session = request.getSession();
		String category_id = request.getParameter("YPCategoryId");
		String city = request.getParameter("YPCity");
		String zip = request.getParameter("YPZip");
		String search = request.getParameter("YPSearchString");
		String name = request.getParameter("YPName");
		ArrayList listings = getCategoryDetails(category_id, name, city, zip,
				search);
		session.setAttribute("YPListings", listings);
		return mapping.findForward(PortalConstants.PAGE_SUCCESS);
	}

	public ArrayList getCategoryDetails(String categoryId) throws Exception {
		return getCategoryDetails(categoryId, null, null, null);
	}

	public ArrayList getCategoryDetails(String categoryId, String name,
			String city, String zip) throws Exception {
		return getCategoryDetails(categoryId, name, city, zip, null);
	}

	public ArrayList getCategoryDetails(String categoryId, String name,
			String city, String zip, String searchString) throws Exception {

		DBConnectionImpl dbConn = new DBConnectionImpl();
		Connection conn = null;
		try {

			conn = dbConn.getConnection();

		} catch (Exception e) {
			throw e;
		}

		Statement stmt = conn.createStatement();
		String sqlStatement = "select * from items_yp where";

		if (categoryId == null) {
			sqlStatement = "select * from items_yp where city='" + city + "'";
		} else {

			if (categoryId != null) {

				int catId = Integer.parseInt(categoryId);
				sqlStatement = sqlStatement + " category_id=" + catId;
			}

			if (city != null && city.trim().length()>0)
				sqlStatement = sqlStatement + " and city='" + city + "'";

			if (zip != null && zip.trim().length()>0)
				sqlStatement = sqlStatement + " and zip='" + zip + "'";

			if (searchString != null)
				sqlStatement = sqlStatement + " and notes like '%"
						+ searchString + "%'";

		}

		ResultSet rs = stmt.executeQuery(sqlStatement);
		ArrayList items = new ArrayList();
		while (rs.next()) {
			YPListingsForm ypFormBean = new YPListingsForm();
			String itemId = rs.getInt("item_id") + "";
			String name_ = rs.getString("name");

			if (name != null) {
				if (name_.toLowerCase().indexOf(name.toLowerCase()) == -1) {
					continue;
				}
			}

			String address = rs.getString("address");
			String city_ = rs.getString("city");
			String phone = rs.getString("phone");
			String state = rs.getString("state");
			String zip_ = rs.getString("zip");
			String category_id = rs.getString("category_id");
			String notes = rs.getString("notes");
			String email = rs.getString("email");
			String url = rs.getString("url");

			ypFormBean.setItem_id(itemId);
			ypFormBean.setName(name_);
			ypFormBean.setAddress(address);
			ypFormBean.setCity(city_);
			ypFormBean.setPhone(phone);
			ypFormBean.setState(state);
			ypFormBean.setZip(zip_);
			ypFormBean.setCategory_id(category_id);
			ypFormBean.setNotes(notes);
			ypFormBean.setEmail(email);
			ypFormBean.setUrl(url);
			items.add(ypFormBean);
		}

		rs.close();
		stmt.close();

		return items;
	}

}